<?php 
/**
* 
*/
class Model_Index extends Yjnic_Model{

	protected $_table = '';

	public function __construct(){
		parent::__construct(DI()->config->get('dbs.servers.default'));
		$this->_table = $this->_prefix . 'user';
	}

	public function getUsers(){
		$sql = "SELECT * FROM ".$this->_table;
		return $this->fetchAll($sql);
	}

	public function test($id){
		$data = array();
		$data['email'] = "email".time();
		try {
			$condition = array(
				'idid in (?)',
				array('1,2,3')				
			);
			// echo 'update:'. $this->update($data, $condition, $this->_table);echo "<br/>";//返回影响行数，第二个参数为数组
			// echo 'update:'. $this->update($data, 'user_id = 1', $this->_table);//返回影响行数,第二个参数为字符串
			// echo 'update sql' . $this->getLastSql();echo "<br/>";
		} catch (Exception $e) {//调试语句
			// echo 'sql error' . $this->getLastSql();echo "<br/>";
		}
		// 添加
		$data = array();
		$data['username'] = "user3";
		$data['email'] = "email";
		$ret = $this->insert($data, $this->_table);
		// var_dump($ret);echo "<br/>";
		// 删除
		$ret = $this->delete('user_id='.$ret, $this->_table);
		// var_dump($ret);echo "<br/>";
		$sql = "SELECT * FROM ".$this->_table." WHERE user_id = ?";
		$params = array(
			$id
		);
		return $this->getRow($sql, 'user_id', $params ); 
	}

	public function getOne($id){
		$sql = "SELECT * FROM ".$this->_table." WHERE user_id = ?";
		$params = array(
			$id
		);
		return $this->fetchRow($sql, $params ); 
	}

	public function test2(){
		// $ret = $this->table('user')->selectRow();
		// var_dump($ret);
		// $ret = $this->table('user')->limit(1)->select();
		// var_dump($ret);
		// $ret = $this->table('user')->findAll('username');
		// var_dump($ret);
		// $ret = $this->table('user')->findRow('username');
		// var_dump($ret);
		// $ret = $this->table('user')->findOne('username');
		// var_dump($ret);
		// $data = array('username'=>'username3', 'age'=>10);
		// $user_id = $this->insert($data);
		// // var_dump($user_id);die;
		// $ret = $this->where('user_id=?',$user_id)->save($data);
		// var_dump($ret);
		// $ret = $this->where('user_id=?',$user_id)->delete();
		// var_dump($ret);
		$params=array(2,'username2');
		$ret = $this->where('user_id in (?,?,?)', [3,4,5])->order('user_id asc')->select();
		var_dump($ret);
		$ret = $this->where('user_id in (?,?,?)', [3,4,5])->order('user_id desc')->select();
		var_dump($ret);
	}
}
 ?>